<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="线路code" prop="lineCode">
        <el-input
          v-model="queryParams.lineCode"
          placeholder="请输入线路code"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="线路名称" prop="lineName">
        <el-input
          v-model="queryParams.lineName"
          placeholder="请输入线路名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="区域" prop="areaCode">
        <el-select
          v-model="queryParams.areaCode"
          placeholder="请选择区域"
          clearable
        >
          <el-option
            v-for="areaOne in areaAlls"
            :key="areaOne.areaCode"
            :label="areaOne.areaName"
            :value="areaOne.areaCode"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="线路起点" prop="lineStart">
        <el-input
          v-model="queryParams.lineStart"
          placeholder="请输入线路起点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="线路终点" prop="lineEnd">
        <el-input
          v-model="queryParams.lineEnd"
          placeholder="请输入线路终点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电压等级" prop="voltageLevel">
        <el-input
          v-model="queryParams.voltageLevel"
          placeholder="请输入电压等级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="线路长度" prop="lineLength">
        <el-input
          v-model="queryParams.lineLength"
          placeholder="请输入线路长度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yanjing:line:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yanjing:line:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yanjing:line:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yanjing:line:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="lineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="线路code" align="center" prop="lineCode" />
      <el-table-column label="线路名称" align="center" prop="lineName" />
      <el-table-column label="区域名称" align="center" prop="areaName" />
      <el-table-column label="线路起点" align="center" prop="lineStart" />
      <el-table-column label="线路终点" align="center" prop="lineEnd" />
      <el-table-column label="电压等级" align="center" prop="voltageLevel" />
      <el-table-column label="线路长度" align="center" prop="lineLength" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yanjing:line:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yanjing:line:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改线路信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="线路code" prop="lineCode">
          <el-input v-model="form.lineCode" placeholder="请输入线路code" />
        </el-form-item>
        <el-form-item label="线路名称" prop="lineName">
          <el-input v-model="form.lineName" placeholder="请输入线路名称" />
        </el-form-item>
<el-form-item label="区域" prop="areaCode">
              <el-select
                v-model="form.areaCode"
                placeholder="请选择区域"
                clearable
              >
                <el-option
                  v-for="areaOne in areaAlls"
                  :key="areaOne.areaCode"
                  :label="areaOne.areaName"
                  :value="areaOne.areaCode"
                ></el-option>
              </el-select>
            </el-form-item>
        <el-form-item label="线路起点" prop="lineStart">
          <el-input v-model="form.lineStart" placeholder="请输入线路起点" />
        </el-form-item>
        <el-form-item label="线路终点" prop="lineEnd">
          <el-input v-model="form.lineEnd" placeholder="请输入线路终点" />
        </el-form-item>
        <el-form-item label="电压等级" prop="voltageLevel">
          <el-input v-model="form.voltageLevel" placeholder="请输入电压等级" />
        </el-form-item>
        <el-form-item label="线路长度" prop="lineLength">
          <el-input v-model="form.lineLength" placeholder="请输入线路长度" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getLine, delLine, addLine, updateLine,selectLineAndAreaInfoList } from "@/api/line/line.js";
import { listArea } from "@/api/line/area.js";

export default {
  name: "Line",
  data() {
    return {
      areaAlls:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线路信息表格数据
      lineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lineCode: null,
        lineName: null,
        areaCode: null,
        areaName: null,
        lineStart: null,
        lineEnd: null,
        voltageLevel: null,
        lineLength: null,
        standby1: null,
        standby2: null,
        standb3: null,
        createEmployeeId: null,
        updateEmployeeId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.listArea();
    this.getList();
  },
  methods: {
        // 加载一级数据
    listArea() {
      listArea().then((response) => {
        this.areaAlls = response.rows;
      });
    },
    /** 查询线路信息列表 */
    getList() {
      this.loading = true;
      selectLineAndAreaInfoList(this.queryParams).then(response => {
        this.lineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lineCode: null,
        lineName: null,
        areaCode: null,
        areaName: null,
        lineStart: null,
        lineEnd: null,
        voltageLevel: null,
        lineLength: null,
        standby1: null,
        standby2: null,
        standb3: null,
        createTime: null,
        createEmployeeId: null,
        updateTime: null,
        updateEmployeeId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线路信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改线路信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除线路信息编号为"' + ids + '"的数据项？').then(function() {
        return delLine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yanjing/line/export', {
        ...this.queryParams
      }, `line_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
